/* Mohamed Zaid Bhura
 * CSE 2102
 * Lab 1
 * This is the CustomRadioButton class which is a component of the ButtonPanel.
 */

import javax.swing.*;
import java.awt.Color;

public class CustomRadioButton extends JRadioButton {
	private Color _color;
	private String _colorName;
	private DemoPanel _panel;
	private CustomRadioButton _button;

	public CustomRadioButton(Color aColor, String aColorName,
			ButtonGroup aGroup, boolean isSelected, DemoPanel aPanel) {
		super(aColorName, isSelected);
		_color = aColor;
		_panel = aPanel;
		_button = this;
		this.setBackground(aColor);
		this.addActionListener(new ColorListener());	// Creates and adds a new ColorListener.
		aGroup.add(this);
	}

	private class ColorListener implements java.awt.event.ActionListener {
		public void actionPerformed(java.awt.event.ActionEvent e) {
			_color = JColorChooser.showDialog(null, "Select a Color", _color);	// Displays a custom color chooser window.
			if (_color == null)	
				_color = Color.GRAY;	// Sets it to gray if no color is selected.
			_panel.setColor(_color);
			_button.setBackground(_color);
		}
	}
	
	public String getColorName() {
		return _colorName;
	}

	public Color getColor() {
		return _color;
	}

}